using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(UserDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:UserDataSource runat=server></{0}:UserDataSource>")]
    public class UserDataSource : GenericDataSource<Bll.User>
    {
        public enum Fields
        {
            Id,
            UserName,
            FirstName,
            LastName,
            DateOfBirth,
            EmailAddress,
            Gender,
            Address1,
            Address2, 
            Town,
            PostCode,
            MobileTel,
            ConcurencyId
        }

        protected override Acms.Bll.User PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Acms.Bll.User tmp = null;
            object concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                var args = new CreateUserEventArgs();
                if (CreateUser != null)
                {
                    CreateUser(this, args);
                }
                else if (this.Page is AcmsControls.TemplatePage)
                {
                    args.User = Bll.User.CreateInstance(((AcmsControls.TemplatePage)Page).AcmsContext.SiteId);
                }
                if (args.User == null)
                {
                    throw new ArgumentNullException("CreateUser event does not populate user property");
                }
                else
                {
                    tmp = args.User;
                }
            }
            else if (action == UpdateAction.Update || action == UpdateAction.Delete)
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<object>(Fields.ConcurencyId, keys);

                tmp = Acms.Bll.User.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);
                tmp.UserName = SetValue(tmp.UserName, Fields.UserName, values, oldValues);
                tmp.FirstName = SetValue(tmp.FirstName, Fields.FirstName, values, oldValues);
                tmp.LastName = SetValue(tmp.LastName, Fields.LastName, values, oldValues);
                tmp.DateOfBirth = SetValue(tmp.DateOfBirth, Fields.DateOfBirth, values, oldValues);
                tmp.EmailAddress = SetValue(tmp.EmailAddress, Fields.EmailAddress, values, oldValues);
                tmp.Gender = SetValue(tmp.Gender, Fields.Gender, values, oldValues);
                tmp.Address1 = SetValue(tmp.Address1, Fields.Address1, values, oldValues);
                tmp.Address2 = SetValue(tmp.Address2, Fields.Address2, values, oldValues);
                tmp.Town = SetValue(tmp.Town, Fields.Town, values, oldValues);
                tmp.PostCode = SetValue(tmp.PostCode, Fields.PostCode, values, oldValues);
                tmp.MobileTel = SetValue(tmp.MobileTel, Fields.MobileTel, values, oldValues);
            }

            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.User item)
        {
            ((Acms.Bll.User)item).Update();
            return 1;
        }

        protected override int Delete(Acms.Bll.User item)
        {
            item.Delete();
            return 1;
        }

        public class CreateUserEventArgs : System.EventArgs
        {
            public Acms.Bll.User User { get; set; }
        }

        public event EventHandler<CreateUserEventArgs> CreateUser;

    }


    public class UserDataSourceDesigner : GenericDataSourceDesigner<Bll.User>
    {

        protected override Acms.Bll.User GetDesignTimeItem(int index)
        {
            Acms.Bll.User user = Acms.Bll.User.CreateInstance(0);
            return user;
        }
    }
}
